package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.redis.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.repository.UserInfoRepository;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.JsonResult;
import com.alibaba.fastjson.JSON;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@RestController
public class LoginController {

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    //测试查询到的用户信息能不能正常传到前台显示
    @RequestMapping("/get")
    public UserInfo get(String id) {
        UserInfo userInfo = userInfoService.get(id);
        return userInfo;
    }

    @GetMapping("/checkPhone")
    public Object checkPhone(String phone) {
        if (phone == null) {
            throw new LogicException("手机号码不能为null");
        }
        boolean ret = userInfoService.checkPhone(phone);
        return ret;
    }


    @PostMapping("/userRegist")
    public Object userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        userInfoService.userRegist(phone, nickname, password, rpassword, verifyCode);
        return JsonResult.success();
    }

    //引入配置文件中的数据
    @Value("${sms.url}")
    private String smsUrl;
    @Value("${sms.appkey}")
    private String appkey;

    //短信发送
    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone) {
        userInfoService.sendVerifyCode(phone, smsUrl, appkey);
        return JsonResult.success();
    }


    //用户登录
    @PostMapping("/userLogin")
    public Object userLogin(String username, String password ) {
        //1、登录操作: 去mongodb中查询用户信息，返回userInfo
        UserInfo userInfo =  userInfoService.userLogin(username, password);

        //2、创建token：随机的UUID
        String token = UUID.randomUUID().toString().replace("-", "");
        //3、将用户信息保存到Redis中：token做为key，对象做为value
        userInfoRedisService.setToken(token, userInfo);
        /*
         * 应该返回什么样的数据：看前台怎么处理登录过程的回调参数的
         * 根据前台参数推导数据格式应该是这样的：
         *   data：{
         *      code:200,
         *      data:{
         *          token:xxx,
         *          user:xxx,
         *          msg:'xxxxxx'
         *      }
         *   }
         */
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("user", userInfo);
        //JsonResult中success方法使用泛型巧妙的创建出了前台需要的数据。通用性
        return JsonResult.success(map);
    }

    //当前用户的信息：页面显示头像用户名等信息
    @GetMapping("/currentUser")
    @RequireLogin
    public Object currentUser(HttpServletRequest request){
        //token存在请求头中
        String token = request.getHeader("token");
        UserInfo info = userInfoRedisService.getUserInfoByToken(token);
        return JsonResult.success(info);
    }


}
